package com.raymond.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.raymond.bean.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;

/**
 * redis生产者
 *
 * @author :  raymond
 * @version :  1.0
 * @date :  2019/7/17
 */
@Component
@Slf4j
public class RedisProducer {
    private final JedisCluster jedisCluster;

    public RedisProducer(JedisCluster jedisCluster) {
        this.jedisCluster = jedisCluster;
    }


    /**
     * 存放消息到redis
     * @param key key
     * @param value value
     */
    public void producers(String key, String... value) {
        jedisCluster.lpush(key, value);
    }


    /**
     * 存放消息到redis
     * @param key key
     * @param value value
     */
    public void producers(String key, Message... value) {
        jedisCluster.lpush(key.getBytes(), JSONObject.toJSONBytes(value));
    }

    private String[] getStringArray(Message... value){
        String[] strings = new String[value.length];
        for (int i = 0; i < value.length; i++) {
            strings[i] = JSON.toJSONString(value);

        }
        return strings;
    }

}
